
import { App } from "../../../App";
import AbstractComponent from "../../../base/AbstractComponent";
import AbstractController from "../../../base/AbstractController";
import CommConst from "../../../const/CommConst";
import GameConst from "../../../const/GameConst";
import GameRespBody from "../../../const/GameRespBody";
import wsTool from "../../../net/ws";
import { showErrorTip, showTip } from "../../../tools/PopuUp";
import UtilTool from "../../../tools/UtilTool";
import { GameBaseModel } from "../GameBaseModel";
import { CLIENTMSG_SWITCH, GAME_MULTI_TYPE } from "../GameManage";
import { GameModel1 } from "../mode1/GameModel1";
import { GameModel3 } from "../mode3/GameModel3";
import { GameModel4 } from "../mode4/GameModel4";
import { GameModel5 } from "../mode5/GameModel5";


export default class BilliardsGameController extends AbstractController
{
    private gameView:GameBaseModel;
    
    public init(view:GameBaseModel) {
        this.view = view;
        this.gameView = view;
        super.init(view);

    }   
    
    /* public send(buf) {
        GameModule.instance.gameSocketController.send(buf);
    } */
    /**请求服务器数据 */

    /**请求认输 */
    public reqGiveUpMsg(cb) {
        if (CLIENTMSG_SWITCH) {

            return
        }

        wsTool.send({"msgCode":GameConst.Req_GIVEUP})
        .then(res =>{
            if (res?.result == 0) {
                cb && cb()
                UtilTool.printLog("请求认输成功")
            }else {
                showErrorTip(res.result)
            }

        })
    }

    /**请求客户端开球 */
    public reqOpenBallMsg(allBallInfo) {
        if (CLIENTMSG_SWITCH) {
            this.respGameMsg(GameRespBody.GameStatusChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_OPEN_BALL,"allBallInfo":allBallInfo})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("请求开球成功")
            }else {
                showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求调整球杆角度
     * @param cueAngle 球杆的角度
     */
    public reqChangeCueMsg(cueAngle) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_CHANGE_CUEANGLE,"angle":cueAngle, "noLoad":true})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("请求调整角度成功")
            }else {
                // showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求玩家击球
     * @param angle 球的角度
     * @param dynamics 力度
     */
    public reqHitballMsg(angle, dynamics) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        let ballActionP = [App.Model.gameManage.ballActionPoint.x, App.Model.gameManage.ballActionPoint.y]
        let ballActionDial = App.Model.gameManage.ballActionDial
        wsTool.send({"msgCode":GameConst.Req_HIT_BALL,"angle":angle ,"dynamics":dynamics, "ballActionP":ballActionP ,"ballActionDial":ballActionDial})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("请求开始击球成功")
            }else {
                showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求球桌运动结束，上报击球结果
     * @param motherBall 0:母球正常打球 1:母球没有击中任何球 2:母球先击中别家球 3:母球进洞了
     * @param enterBall 进洞的球号
     * @param allBallInfo 各个球的信息
     * @param wheel 轮数,标识着是上传的第几轮的击球结果
     */
    public reqHitballEndMsg(motherBall, enterBall, allBallInfo, wheel) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_HIT_BALL_END,"motherBall":motherBall, "enterBall":enterBall, "allBallInfo":allBallInfo ,"wheel":wheel})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("上报击球结果成功")
            }else {
                // showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求上报帧数据
     * @param allBallInfo 所有球的位置信息
     */
    public reqFrameDataMsg(allBallInfo) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_FRAME_DATA,"allBallInfo":allBallInfo, "noLoad":true})
        .then(res =>{
            if (res?.result == 0) {
                // UtilTool.printLog("上报帧数据成功")
            }else {
                // showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求上报重新摆球的结果
     * @param allBallInfo 所有球的位置信息
     */
    public reqPosBallResoultMsg(allBallInfo) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_POSBALL_RESOULT,"allBallInfo":allBallInfo})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("上报重新摆球结果成功")
            }else {
                showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求退出观战
     */
    public reqQuitWatchMsg() {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_QUIT_WATCH})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("请求退出观战成功")
                App.Model.gameManage.quitWatch()
            }else {
                showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求查询玩家剩余台球
     * @param roomId 房间号
     * @param roomType 房间类型
     */
    public reqUsersBallListMsg(roomId, roomType, cb) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_USERS_TARGETBALL, "roomId":roomId, "roomType":roomType})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("请求查询玩家剩余台球成功")
                cb && cb(res)
            }else {
                showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求球桌运动结束，上报击球结果（四球开伦）
     * @param wheel 轮数,标识着是上传的第几轮的击球结果
     * @param hitBallNum 碰撞到的球号
     * @param allBallInfo 各个球的信息
     */
    public reqHitballEndFourMsg(wheel, hitBallNum, allBallInfo) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_FOUR_HITBALL_END,"wheel":wheel, "hitBallNum":hitBallNum, "allBallInfo":allBallInfo})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("上报击球结果成功")
            }else {
                // showErrorTip(res.result)
            }
        })
    }

    /**
     * 请求球桌运动结束，上报击球结果（血流到底）
     * @param wheel 轮数,标识着是上传的第几轮的击球结果
     * @param allBallInfo 各个球的信息
     * @param enterBall 进洞的球号
     * @param areaList  触碰到的区域的编号
     * @param holeList  触碰到的球洞的编号
     */
    public reqHitballEndBloodMsg(wheel, allBallInfo, enterBall, areaList, holeList) {
        if (CLIENTMSG_SWITCH) {
            // this.respGameMsg(GameRespBody.cueChange_Data)
            return
        }

        wsTool.send({"msgCode":GameConst.Req_BLOOD_HITBALL_END,"wheel":wheel, "allBallInfo":allBallInfo, "enterBall":enterBall, "areaList":areaList, "holeList":holeList })
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("上报击球结果成功")
            }else {
                // showErrorTip(res.result)
            }
        })
    }

    private m_bGameStatus:number;
    private m_bAllowLookon:boolean;
    /**处理框架消息数据 */
    public respSceneMsg(sid,buf,len):void
    {
        UtilTool.printLog("-----框架消息::::",sid)
        // switch (sid)
        // {
        //     case GameConst.SUB_GF_OPTION:     //游戏配置
        //     {
             
        //         break;
        //     }
        //     case GameConst.SUB_GF_SCENE:      //游戏场景
        //     {
        //         if (this.m_bGameStatus==GameConst.GS_FREE)
        //         {
        //             UtilTool.printLog("游戏场景--空闲状态");                    
                    
        //         }
        //         else if(this.m_bGameStatus==GameConst.KWX_GS_MJ_PLAY)
        //         {
        //             UtilTool.printLog("游戏场景--游戏状态");

                    
        //         }
        //         else if(this.m_bGameStatus == GameConst.WeChatKWX_GS_MJ_END )
        //         {
        //             UtilTool.printLog("游戏场景--结算状态");
                    
        //         }

        //         break;
        //     }
        //     case GameConst.SUB_GF_AGAIN_GAME:   // 再来一局index
        //     {
                
        //         break;
        //     }
        //     case GameConst.SUB_GF_MESSAGE:		//系统消息
        //     {
        //         this.showMessage(buf);
        //         break;
        //     }
        //     default:
        //     {
        //         break;
        //     }
        // }
    }
    /**处理游戏消息数据 */
    public respGameMsg(data /* sid,buf,len */):void
    {
        let gameView = null
        // if (App.Model.gameManage.gameType == GAME_MULTI_TYPE.PT_MULIT || App.Model.gameManage.gameType == GAME_MULTI_TYPE.ZY_MULIT){
            gameView = App.Model.gameView as GameModel3 | GameModel4 | GameModel5
        // }
        // else if (App.Model.gameManage.gameType == GAME_MULTI_TYPE.PT_FOURBALL || App.Model.gameManage.gameType == GAME_MULTI_TYPE.ZY_FOURBALL){
        //     gameView = App.Model.gameView as GameModel4
        // }
        // else{
        // }

        if (gameView==null || gameView.gameLogic==null) return
        if (App.Model.gameView==null) {
            return
        }

        switch (data.msgCode)
        {
            case GameConst.Resp_GAME_START:// 游戏开始
            {
                // this.onGameStart(buf,len);
                // this.gameView.gameStart()
                break;
            }

            case GameConst.Resp_ROOM_DESTROY: // 房间销毁
            {
                gameView.gameLogic.respRoomDestory(data)
                break;
            }

            case GameConst.Resp_ROOM_INFOCHANGE:// 房间信息发送变化
            {
                App.Model.gameManage.roomInfoChange = data
                break;
            }

            case GameConst.Resp_GAME_STATUS:// 通知玩家当前房间内游戏状态
            {
                gameView.gameLogic.respGameStatusChange(data)
                break;
            }

            case GameConst.Resp_CHANGE_CUEANGLE:// 通知玩家球杆角度发生变化
            {
                gameView.gameLogic.respCueChange(data)
                break;
            }

            case GameConst.Resp_HIT_BALL:// 通知房间内玩家当前玩家击球了
            {
                if (data.ballActionP){
                    App.Model.gameManage.setActionPoint_other(data.ballActionP)
                    App.Model.gameManage.setBallActionDial_other(data.ballActionDial)
                }
                gameView.gameLogic.respHitball(data)
                break;
            }

            case GameConst.Resp_POSBALL_RESOULT:// 通知重新摆球的结果
            {
                gameView.gameLogic.respPosBallResoult(data)
                break;
            }

            case GameConst.Resp_GAME_END:// 通知房间内玩家多人游戏结束
            {
                gameView.gameLogic.respGameEnd(data)
                break;
            }

            case CommConst.Resq_ROOM_KICK_OUT:// 通知玩家被踢出房间
            {
                gameView.gameLogic.respRoomKickOut(data)
                break;
            }

            case GameConst.Resp_FRAME_DATA:// 通知玩家帧数据
            {
                gameView.gameLogic.respFrameData(data)
                break;
            }

            case GameConst.Resp_FOURGAME_STATUS:// (四球开伦)通知玩家当前房间内游戏状态
            {
                gameView.gameLogic.respGameStatusChange(data)
                break;
            }

            case GameConst.Resp_BLOODGAME_STATUS:// (血流到底)通知玩家当前房间内游戏状态
            {
                gameView.gameLogic.respGameStatusChange(data)
                break;
            }

            default:
            {
                break;
            }
        }
    }
    

    ////////////游戏消息---------------------------------------
    /**收到游戏开始消息 */
    private onGameStart(buf,len):void
    {
        
    }
    
}